﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BISPOS.Repository
{
    public class StockRepository
    {
        BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
        ProductRepository productRepository = new ProductRepository();
        public void EditStock(int stockId, double originalQuantity, double currentQuantity, decimal buyingCost, DateTime date)
        {
            Stock stock = GetStock(stockId);
            stock.OriginalQuantity = originalQuantity;
            stock.CurrentQuantity = currentQuantity;
            stock.BuyingCost = buyingCost;
            stock.Date = date;
            Save();
        }
        
        
        public Stock AddNewStock(int productId, double originalQuantity, double currentQuantity, decimal buyingCost, DateTime date)
        {
            Stock newStock = new Stock
            {
                ProductId = productId,
                OriginalQuantity = originalQuantity,
                CurrentQuantity = currentQuantity,
                BuyingCost = buyingCost,
                Date = date
            };
            db.Stocks.InsertOnSubmit(newStock);
            Save();
            return newStock;
        }
        public Stock GetStock(int id)
        {
            return db.Stocks.FirstOrDefault(x=>x.Id == id);
        }
        public int SubtractStock(int stockId, double quantity)
        {
            Stock stock = GetStock(stockId);
            stock.CurrentQuantity -= quantity;
            Save();
            return stockId;
            
        }
        public void Save() { db.SubmitChanges(); }
    }
}
